// pages/config/config.js
import {storage} from "../../utils/storage";
const app = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
      regionList:[
          { regionId: 'cn-shanghai',isSelected:0 },
          { regionId: 'cn-qingdao',isSelected:0 },
          { regionId: 'cn-beijing',isSelected:0 },
          { regionId: 'cn-zhangjiakou',isSelected:0 },
          { regionId: 'cn-huhehaote',isSelected:0 },
          { regionId: 'cn-hangzhou',isSelected:0 },
          { regionId: 'cn-shenzhen',isSelected:0 },
          { regionId: 'cn-heyuan',isSelected:0 },
          { regionId: 'cn-chengdu',isSelected:0 },
          { regionId: 'cn-hongkong',isSelected:0 },
      ],

      AccessKeyId: storage.get('AccessKeyId') ||'',
      AccessKeySecret: storage.get('AccessKeySecret') ||'',
      ProductKey: storage.get('ProductKey') ||'',
      selectRegion: storage.get('regionId') || 'cn-shanghai',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
      this.updateRegion(this.data.selectRegion);
  },

  handleProductKey(e) {
      this.setData({
          ProductKey: e.detail.value
      });
  },

  handleAccessKeyId(e) {
      this.setData({
          AccessKeyId: e.detail.value
      });
  },

  handleAccessKeySecret(e) {
      this.setData({
          AccessKeySecret: e.detail.value
      });
  },

  selectRegion(e) {
    let { region } = e.currentTarget.dataset;
    this.updateRegion(region);
  },

  updateRegion(selectRegion) {
      this.data.regionList.forEach(item => {
          if (item.regionId === selectRegion){
              item.isSelected = 1;
          } else {
              item.isSelected = 0;
          }
      });
      this.setData({
          selectRegion: selectRegion,
          regionList: this.data.regionList,
      });
  },

  submit() {
      if (!this.data.ProductKey){
          wx.showToast({ title: '请填写产品ID', icon: 'none' });
          return;
      }
      if (!this.data.AccessKeyId){
          wx.showToast({ title: '请填写访问服务ID', icon: 'none' });
          return;
      }
      if (!this.data.AccessKeySecret){
          wx.showToast({ title: '请填写访问服务密钥', icon: 'none' });
          return;
      }
      if (!this.data.selectRegion){
          wx.showToast({ title: '请选择设备所在地域', icon: 'none' });
          return;
      }

      storage.setSync('AccessKeyId', this.data.AccessKeyId);
      storage.setSync('AccessKeySecret', this.data.AccessKeySecret);
      storage.setSync('ProductKey', this.data.ProductKey);
      storage.setSync('selectRegion', this.data.selectRegion);

      app.aliConfig.RegionId = this.data.selectRegion;
      app.aliConfig.AccessKeyId = this.data.AccessKeyId;
      app.aliConfig.AccessKeySecret = this.data.AccessKeySecret;
      app.aliConfig.ProductKey = this.data.ProductKey;
      app.aliConfig.EndPoint = `https://iot.${storage.get('regionId') || 'cn-shanghai'}.aliyuncs.com/`;

      wx.showToast({ title: '保存成功', icon: 'none' });

      setTimeout(() => {
          wx.navigateBack({
              delta: 1
          });
      },1500);
  }

});
